/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package nl.rainbowsheep.reisorg.rest.service.service;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import nl.rainbowsheep.reisorg.rest.service.model.Customer;
import nl.rainbowsheep.reisorg.rest.service.model.TravelDocumentType;
import nl.rainbowsheep.reisorg.rest.service.model.Trip;
import nl.rainbowsheep.reisorg.rest.service.model.TripBooking;
import nl.rainbowsheep.reisorg.rest.service.repository.TripBookingRepository;
import nl.rainbowsheep.reisorg.rest.service.viewmodel.TripBookingViewmodel;
import nl.rainbowsheep.reisorg.rest.service.viewmodel.TripViewmodel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author ieme
 */
@Service
public class TripBookingService {
    
    @Autowired
    TripBookingRepository tripBookingRepo;
    @Autowired
    CustomerService customerService;
    @Autowired
    TripService tripService;
    @Autowired
    TravelDocumentTypeService travelDocumentTypeService;
    
    public TripBooking save(TripBooking tripbooking) {
        //status van 10 is active & not paid
        tripbooking.setStatus(10);
        tripbooking.setHasTravelDocument(false);
        return tripBookingRepo.save(tripbooking);
    }
    
    public List<TripBookingViewmodel> findBookings(long customerId) {
        List<TripBooking> tripbookings = tripBookingRepo.findByCustomerId(customerId);
        List<TripBookingViewmodel> tripbookingViewmodels = new ArrayList();
        for (TripBooking tripbooking : tripbookings) {
            TripBookingViewmodel tbv = new TripBookingViewmodel();
            try {
                Customer c = customerService.getCustomerById(customerId);
                tbv.setCustomer(c);
            } catch (Exception e) {
                e.toString();
            }
            try {
                TripViewmodel tripViewmodel = tripService.findById(tripbooking.getTripId());
                tbv.setTrip(tripViewmodel);
            } catch (Exception e) {
                e.toString();
            }
            if (tripbooking.getTravelDocumentTypeId() != null) {
                try {
                    TravelDocumentType travelDocumentType = travelDocumentTypeService.findById(tripbooking.getTravelDocumentTypeId());
                    tbv.setTravelDocumentType(travelDocumentType);
                } catch (Exception e) {
                    e.toString();
                }
            }
            tbv.setTripbooking(tripbooking);
            tripbookingViewmodels.add(tbv);
        }
        
        return tripbookingViewmodels;
    }
}
